//
//  StoreManage
//
//  Created by MC on 2018/2/6.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCSpecificationsViewController: MCViewController {
    
    var goodsId = 0
    var type = 0   // 0平台商品   1自建商品
    var isEditSepc = false    // 是否编辑规格  不是的话，隐藏删除按钮
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        sendNetworking()
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc override func leftItemClicked() {
        
        if self.dataArrayM.count != 0 {
            for i in 0..<(self.navigationController?.viewControllers.count)! {
                
                if self.navigationController?.viewControllers[i].isKind(of: MCgoodsListEditViewController.self) == true {
                    
                    _ = self.navigationController?.popToViewController(self.navigationController?.viewControllers[i] as! MCgoodsListEditViewController, animated: true)
                    break
                }
            }
        } else {
            MCAlterController_twoOptions(vc: self, message: "该商品没有规格，将成为无效商品。\n是否删除该商品？", confirm: {
                print("删除商品")
                
                let para = [
                    "goodsId" : self.goodsId,
                    ] as [String : Any]
                
                MCNetwork_post(url: JAVA_deleteInvalidGoods, para: para as [String : AnyObject], vc: self,hudSuperView: self.view,isHiddenTabbar: true, success: { (response) in
                    
                    MCHUD_text(message: "成功删除该商品", view: self.view, delay: 1.2)
                    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.2, execute: {
  
                        
                        for i in 0..<(self.navigationController?.viewControllers.count)! {
                            
                            if self.navigationController?.viewControllers[i].isKind(of: MCgoodsListEditViewController.self) == true {
                                
                                _ = self.navigationController?.popToViewController(self.navigationController?.viewControllers[i] as! MCgoodsListEditViewController, animated: true)
                                break
                            }
                        }
                        
                        
                    })
                    
                }, dislike: { (response) in }) { (error) in }
                
                
            }, cancle: {
                self.navigationController?.popViewController(animated: true)
            })
            
            
            self.navigationController?.popViewController(animated: true)

        }
        
    }

    
    @objc func addButtonClicked() {
        let vc = MCEditSpecificationsViewController()
        vc.goodsId = goodsId
        vc.isAddSepc = true
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }
    // MARK: - Network
    func sendNetworking() {
        let para = [
            "goodsId" : goodsId,
            "type"    : type
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_getGoodsSpecDesc, para: para as [String : AnyObject], vc: self,hudSuperView: self.view,isHiddenTabbar: true, success: { (response) in
            let dataArray = response["data"] as? NSArray ?? []
            
            self.dataArrayM.removeAllObjects()
            
            if dataArray.count == 0 {
                self.tableView.showBgImage()
            } else {
                self.tableView.hideBgImage()
                for dict in dataArray {
                    let model = MCSpecificationsModel()
                    model.setValuesForKeys(dict as! [String : Any])
                    self.dataArrayM.add(model)
                }
            }
            self.tableView.reloadData()
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "商品规格"
        self.view.backgroundColor = UIColor.white
        
        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_image(#imageLiteral(resourceName: "Reuse_add_black"), target: self, selector: #selector(addButtonClicked))

        
        navigationItem.leftBarButtonItem = MCSetBarButtonItem_image(#imageLiteral(resourceName: "Reuse_back"), target: self, selector: #selector(leftItemClicked),isLeft: true)

    }
    
    func initUI() {
        
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view).inset(UIEdgeInsetsMake(0, 0, 0, 0))
        }        
    }
    
    // MARK: - Setter & Getter
    lazy var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
}

extension MCSpecificationsViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArrayM.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCSpecificationsCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        
        cell.model = self.dataArrayM[indexPath.row] as! MCSpecificationsModel
        cell.row = indexPath.row
        cell.delegate = self
        
        return cell
    }
}

extension MCSpecificationsViewController : MCSpecificationsCellDelegate {
    func MCSpecificationsCellEdit(row: Int) {
        let model = self.dataArrayM[row] as! MCSpecificationsModel
        
        let vc = MCEditSpecificationsViewController()
        vc.goodsId = model.goodsId
        vc.model = model
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }
    
    func MCSpecificationsCellDelete(row: Int) {
        let model = self.dataArrayM[row] as! MCSpecificationsModel

        let para = [
            "goodsId" : model.goodsId,
            "productId" : model.productId,
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_deleteSpecDesc, para: para as [String : AnyObject], vc: self,hudSuperView: self.view,isHiddenTabbar: true, success: { (response) in
            
            MCHUD_text(message: "成功删除规格", view: self.view, delay: 1.2)

            self.sendNetworking()
            
        }, dislike: { (response) in }) { (error) in }

    }
}



